<?php
class Controller_Form extends Controller_Admin_Template
{
    protected $_form_type;
    protected $_form_data;
    protected $_list_where;

    // 分页参数
    protected $_list_pagesize;          // 每页多少条
    protected $_list_total_size;        // 总共有多少数据(由子类覆盖这个变量)

    protected $_sort_extra_columns = array();
    protected $_no_buttons = FALSE;     // 控制上方链接区是否要展示
    protected $_no_list_actions = FALSE; // 控制列表区右侧动作区是否要展示
    protected $_pagination_type = 'route';
    protected $_pagination_param = 'param1';
    protected $_formItemsConfig  = NULL;

    public function __construct ($req, $res)
    {
        $this->template = 'smarty:form/form';
        parent::__construct($req, $res);

        $this->_method = $this->request->method();
        $this->_list_total_size = NULL;
        $this->_form = new Form_Editor($this->_form_type, $this);
    }

    public function showListActions()
    {
        return ! ($this->_no_list_actions);
    }

    // 回调函数，返回页面顶部按钮区按钮
    // return array('label' => string, 'class' => string , 'onclick' => string , 'url' => string);
    public function getTopButtons ()
    {
        return NULL;
    }

    public function showTopButtons ()
    {
        return ! ($this->_no_buttons);
    }

    public function action_sort ()
    {
        $post = $this->request->post();
        if (! empty($post))
        {
            $sort = array();
            foreach ($post as $k => $v)
            {
                if (preg_match('/^sort(\d+)$/', $k, $m))
                {
                    $id = $m[1];
                    if ($id > 0)
                        $sort[$id] = (int)$v;
                }
            }

            form_data::set_sorted($this->_form_type, $sort, $this->_sort_extra_columns);
        }

        $this->_form = new Form_Sort($this->_form_type);
        $this->template->set('form_content', $this->_form->build_list($this->_list_where));
        $this->template->set_filename('smarty:form/form');
    }


    public function action_create()
    {
        if ($this->_method == 'POST')
        {
            $id = Form_Data::create($this->_form_type, $this->_form_data);
            $this->after_create($id);
            $this->to_url('detail', $id);
        }
        else
        {
            $this->template->set('form_content', $this->_form->build_form());
            $this->template->set('form_include_edit_file', $this->_form->include_edit_file());
        }
    }

    public function action_update ($id)
    {
        if ($this->_method == 'POST')
        {
            Form_Data::update($this->_form_type, $id, $this->_form_data);
            $this->after_update($id);
            $this->to_url('detail', $id);
        }
        else
        {
            $this->template->set('form_content', $this->_form->set_data($this->_form_data)->build_form());
            $this->template->set('form_include_edit_file', $this->_form->include_edit_file());
        }
    }

    public function action_delete ($id)
    {
        Form_Data::remove ($this->_form_type, $id);
        $this->after_delete($id);
        $this->to_url('list');
    }

    public function action_list ($page = 1)
    {
        $this->_form = new Form_List($this->_form_type, $this->_list_pagesize, $this);
        $this->template->set('form_content', $this->_form->build_list($this->_list_where, $page));
        $this->template->set_filename('smarty:form/form');

        if ($this->_list_pagesize > 0)
        {
            $total = $this->list_count_data();
            $this->template->set('pagination', $this->pagination($total, $this->_list_pagesize,$page_type = 'route', $page_key = 'param1', $views = 'www/page/new'));
        }
        else
        {
            $this->template->set('pagination', '');
        }
    }

    public function action_detail ($id)
    {
        $this->_form = new Form_Detail($this->_form_type, $this);
        $this->template->set('form_content', $this->_form->set_data($this->_form_data)->build_detail());
        $this->template->set_filename('smarty:form/form');
    }

    public function list_filter_data (& $rows)
    {
        //
    }

    public function list_get_data ($offset, $limit)
    {
        return NULL;
    }

    public function list_count_data ()
    {
        if ($this->_list_total_size !== NULL)
            return $this->_list_total_size;
        else
            return $this->_form->get_total_size($this->_list_where);
    }

    public function detail_filter_config_data (& $arr_config)
    {
        //
    }

    public function classname_by_list_row (& $row)
    {
        return '';
    }

    public function actions_by_list_row (& $row, & $menus)
    {
        return array();
    }

    public function getFormItemsConfig ($type)
    {
        return $this->_formItemsConfig;
    }

    protected function build_link ($label, $action, $nid = NULL)
    {
        $link = $this->build_url ($action, $nid);
        if ($action == 'delete')
        {
            return "<a href=\"$link\" onclick=\"return confirm('确认要删除吗?')\">$label</a>";
        }
        else
        {
            return "<a href=\"$link\">$label</a>";
        }
    }


    protected function build_url ($action, $nid = NULL)
    {
        $query = $_SERVER['QUERY_STRING'];
        $controller = $this->request->controller(); 
        $directory  = $this->request->directory();
        $directory  = empty($directory) ? '' : '/'.$directory;
        $query      = empty($query) ? '' : '?'.$query;

        $url = "$directory/$controller/$action";
        if ($nid > 0)
            $url .= "/$nid";

        $url .= $query;

        return $url;
    }

    protected function to_url ($type, $nid = NULL)
    {
        $query = $_SERVER['QUERY_STRING'];
        $controller = $this->request->controller(); 
        $directory  = $this->request->directory();
        $directory  = empty($directory) ? '' : '/'.$directory;
        $query      = empty($query) ? '' : '?'.$query;

        $url = "$directory/$controller/$type";
        if ($nid>0||$nid==0)
            $url .= "/$nid";

        $url .= $query;
        $this->request->redirect($url);
    }

    protected function RMBFen2Yuan(& $moneyFen)
    {
        $moneyFen = '￥'.number_format($moneyFen/100, 2);
    }

    protected function after_update ($id)
    {
        //
    }

    protected function after_create ($id)
    {
        //
    }

    protected function after_delete ($id)
    {
        //
    }
}
